package br.ufpe.cin.rdfilter.main;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import br.ufpe.cin.rdfilter.util.ElementVisitorPredicates;
import br.ufpe.cin.rdfilter.util.ElementVisitorResources;
import br.ufpe.cin.rdfilter.util.FrequencyList;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.sparql.syntax.Element;
import com.hp.hpl.jena.sparql.syntax.ElementWalker;

public class QueriesHandler {

	private ArrayList<Query> queries;

	public QueriesHandler(ArrayList<Query> queries) {
		this.queries = queries;
	}

	/**
	 * @param queries
	 *            the queries to set
	 */
	public void setQueries(ArrayList<Query> queries) {
		this.queries = queries;
	}

	/**
	 * @return the queries
	 */
	public ArrayList<Query> getQueries() {
		return queries;
	}

	/**
	 * @return list sorted by frequency of the resource URIs (subjects and
	 *         objects) of the triple patterns of the queries
	 * @throws URISyntaxException
	 */
	public ArrayList<URI> getResourcesList() throws URISyntaxException {

		FrequencyList<String> frequencyListQuery = new FrequencyList<String>();
		ArrayList<String> resourcesByQuery;

		ArrayList<String> resourcesListString;
		ArrayList<URI> resourcesList = new ArrayList<URI>();

		for (Query query : queries) {

			// getting the pattern of the each query
			Element queryPattern = query.getQueryPattern();

			// create a visitor for finding resources uris in triple pattern
			ElementVisitorResources elementVisitor = new ElementVisitorResources();
			ElementWalker.walk(queryPattern, elementVisitor);

			resourcesByQuery = elementVisitor.getVisitedResources();

			for (String resource : resourcesByQuery) {
				frequencyListQuery.insertFrequency(resource);
			}
		}

		// getting the sorted list of the String of the resources
		resourcesListString = frequencyListQuery.getSortedList();

		// building the list of URIs from the String list
		for (String resource : resourcesListString) {
			resourcesList.add(new URI(resource));
		}

		return resourcesList;
	}

	/**
	 * @return list sorted by frequency of the predicate URIs of the triple
	 *         patterns of the queries
	 * @throws URISyntaxException
	 */
	public ArrayList<URI> getPredicatesList() throws URISyntaxException {

		FrequencyList<String> frequencyListQuery = new FrequencyList<String>();
		ArrayList<String> resourcesByQuery;

		ArrayList<String> predicatesListString;
		ArrayList<URI> predicatesList = new ArrayList<URI>();

		for (Query query : queries) {

			// getting the pattern of the each query
			Element queryPattern = query.getQueryPattern();

			// create a visitor for finding predicates in triple patterns
			ElementVisitorPredicates elementVisitor = new ElementVisitorPredicates();
			ElementWalker.walk(queryPattern, elementVisitor);

			resourcesByQuery = elementVisitor.getVisitedPredicates();

			for (String resource : resourcesByQuery) {
				frequencyListQuery.insertFrequency(resource);
			}
		}

		// getting the sorted list of the String of the predicates
		predicatesListString = frequencyListQuery.getSortedList();

		// building the list of URIs from the String list

		for (String resource : predicatesListString) {
			predicatesList.add(new URI(resource));
		}
		return predicatesList;
	}
}